import {
    AnyType,
    ArrayType,
    BooleanType,
    DateType,
    DecimalType,
    IntegerType,
    IType,
    LongType,
    NumberType,
    RefType,
    Schema,
    StringType
} from './schema.js';


export function schemaNameHandler(name: string): string {
    return name.replace('#/components/schemas/', '').replace('«', '').replace('»', '');
}

/**
 *
 * @return {string} 类型
 */
export function openApiTypeToTypescriptType(schemas: Map<string, Schema>, type?: string, format?: string, ref?: string, items?: any): IType {
    if (ref) {
        // 引用类型
        return new RefType(schemaNameHandler(ref), schemas);
    }
    if (type === 'array') {
        return new ArrayType(openApiTypeToTypescriptType(schemas, items.type, items.format, items.$ref, items.items));
    }
    switch (type) {
        case 'integer':
            switch (format) {
                case 'int32':
                    return new IntegerType();
                case 'int64':
                    return new LongType();
            }
            break;
        case 'number':
            switch (format) {
                case 'float':
                case 'double':
                    return new NumberType();
                case 'number':
                    return new DecimalType();
            }
            break;
        case 'string':
            switch (format) {
                case 'date':
                case 'date-time':
                    return new DateType();
                default:
                    return new StringType();
            }
        case 'boolean':
            return new BooleanType();
    }
    return new AnyType();
}
